/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TCarRent;
import com.gmail.blackdog1987.carcare.entity.TCarSale;
import com.gmail.blackdog1987.carcare.entity.base.Page;
import com.gmail.blackdog1987.carcare.service.CarService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;
import com.gmail.blackdog1987.carcare.util.NullUtil;

/**
 * 
 * @author 董国超
 *
 */
@Service("carServiceImpl")
public class CarServiceImpl implements CarService {
	
	@Autowired
	private BaseJdbcDao baseJdbcDao;

	@Override
	public Map<String, Object> getCarRentList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String order = filter.get("order");
		String type = filter.get("type");
		
		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");
		
		int index = 1;
		int size = 999;
		if(pageIndex!=null&&pageIndex.length()!=0){
			index = Integer.valueOf(pageIndex);
		}
		
		if(index<1){
			index = 1;
		}
		if(pageSize!=null&&pageSize.length()!=0){
			size = Integer.valueOf(pageSize);
		}
		
		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = " from t_car_rent where 1=1 ";
		if(!NullUtil.isEmpty(type)){
			sql += " and type = ? ";
			whereArgs.add(type);
		}
		
		String orderSql = "";
		if(!NullUtil.isEmpty(order)){
			if(order.equals("1")){
				orderSql = " order by price ";
			}else if(order.equals("2")){
				orderSql = " order by buy_time desc ";
			}
		}
		
		
		String limit = " limit " + (index-1)*size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" select count(*) " + sql , whereArgs.toArray());
		
		List<TCarRent> cars = JdbcUtil.queryForList(baseJdbcDao, TCarRent.class, " select * " + sql + orderSql + limit, whereArgs.toArray());
		
		Page page = new Page(cars, index, totalCount, size);
		
		result.put("result", page);
		
		return result;
	}

	@Override
	public Map<String, Object> getCarSaleList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String order = filter.get("order");
		String type = filter.get("type");
		
		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");
		
		int index = 1;
		int size = 999;
		if(pageIndex!=null&&pageIndex.length()!=0){
			index = Integer.valueOf(pageIndex);
		}
		
		if(index<1){
			index = 1;
		}
		if(pageSize!=null&&pageSize.length()!=0){
			size = Integer.valueOf(pageSize);
		}
		
		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = " from t_car_sale where 1=1 ";
		if(!NullUtil.isEmpty(type)){
			sql += " and type = ? ";
			whereArgs.add(type);
		}
		
		String orderSql = "";
		if(!NullUtil.isEmpty(order)){
			if(order.equals("1")){
				orderSql = " order by price ";
			}else if(order.equals("2")){
				orderSql = " order by buy_time desc ";
			}
		}
		
		
		String limit = " limit " + (index-1)*size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" select count(*) " + sql , whereArgs.toArray());
		
		List<TCarSale> cars = JdbcUtil.queryForList(baseJdbcDao, TCarSale.class, " select * " + sql + orderSql  + limit, whereArgs.toArray());
		
		Page page = new Page(cars, index, totalCount, size);
		
		result.put("result", page);
		
		return result;
	}
}
